﻿using System;
using System.Xml.Serialization;

namespace Daco.Core
{
    [Serializable()]
    [XmlRoot("configSections")]
    public class CBaseConfig
    {
        [Serializable()]
        public class CImageSection
        {
            public int ThumbImageSize { get; set; }
            public int LargeImageSize { get; set; }
            public bool StoreImageInDB { get; set; }
            public string AllowedImageExtension { get; set; }
            public string PrefixStringForThumbImage { get; set; }
            public System.Drawing.Imaging.ImageFormat ImageStoreFormat { get; set; }
            public int SizeLimit { get; set; }      // KB

            public CImageSection()
            {
                this.ThumbImageSize = 120;
                this.LargeImageSize = 600;
                this.StoreImageInDB = true;
                this.PrefixStringForThumbImage = "Thumb_";
                this.AllowedImageExtension = "jpg,gif,jpeg,bmp,png";
                this.ImageStoreFormat = System.Drawing.Imaging.ImageFormat.Jpeg;
                this.SizeLimit = 512;       // 512KB
            }
        }
        public class CMembershipSection
        {
            public string HashAlgorithmType { get; set; }
            public int MinPasswordLength { get; set; }
            public int MaxPasswordLength { get; set; }
            public bool RequireUniqueEmail { get; set; }

            public CMembershipSection()
            {
                this.MaxPasswordLength = 16;
                this.MinPasswordLength = 8;
                this.RequireUniqueEmail = true;
                this.HashAlgorithmType = "MD5";
            }
        }

        #region Static
        private static CBaseConfig _current = null;
        public static CBaseConfig Current
        {
            get
            {
                if (_current == null)
                    _current = new CBaseConfig();
                return _current;
            }
        } 
        #endregion

        #region Public Properties
        [XmlAttribute()]
        public string ConnectionStringName { get; set; }

        [XmlIgnore()]
        public string ConnectionString
        {
            get
            {
                return System.Configuration.ConfigurationManager.ConnectionStrings[ConnectionStringName].ConnectionString;
            }
        }

        [XmlAttribute()]
        public string DbSchema { get; set; }

        [XmlAttribute()]
        public string DbObjectQualifier { get; set; }

        [XmlAttribute()]
        public string AppRoot { get; set; }

        [XmlAttribute()]
        public string AppResourceAssembly { get; set; }

        [XmlAttribute()]
        public string IntegerFormatString { get; set; }
        [XmlAttribute()]
        public string DoubleFormatString { get; set; }
        [XmlAttribute()]
        public string AllowedFileAttachmentExtension { get; set; }
        [XmlAttribute()]
        public string FullDbDateTimeFormat { get; set; }

        [XmlElement(ElementName = "membershipSection", IsNullable = true)]
        public CMembershipSection MembershipSection { get; private set; }

        [XmlElement(ElementName = "imageSection", IsNullable = true)]
        public CImageSection ImageSection { get; private set; }

        [XmlAttribute()]
        public string DefaultCulture { get; set; }
        #endregion

        #region Protected Contructors
        protected CBaseConfig()
        {
            this.ConnectionStringName = "DacoConnectionString";
            this.DbObjectQualifier = "";
            this.DbSchema = "dbo";
            this.AppRoot = "";
            this.AppResourceAssembly = "";
            this.MembershipSection = new CMembershipSection();
            this.ImageSection = new CImageSection();
            this.DefaultCulture = "vi-VN";
            this.FullDbDateTimeFormat = "yyyy/MM/dd HH:mm:ss";
            this.DoubleFormatString = "###,###,0.00";
            this.IntegerFormatString = "###,###";
            this.AllowedFileAttachmentExtension = "pdf,doc,xls,txt,rtf";
        } 
        #endregion
    }
}
